package community.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.annotation.Log;
import community.hospital.annotation.Module;
import community.hospital.annotation.NeedLogin;
import community.hospital.annotation.NeedPermission;
import community.hospital.common.Add;
import community.hospital.common.BaseRemove;
import community.hospital.common.R;
import community.hospital.common.Update;
import community.hospital.entity.request.DoctorRequest;
import community.hospital.entity.request.ListDoctorRequest;
import community.hospital.entity.response.DoctorResponse;
import community.hospital.entity.response.OptionResp;
import community.hospital.service.application.DoctorApplicationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static community.hospital.common.Constant.*;
import static community.hospital.common.PermissionConstant.*;

/**
 * @author yindingjie
 */
@RequestMapping("/doctor")
@RestController
@Module("医生")
public class DoctorController {
    @Resource
    private DoctorApplicationService doctorApplicationService;

    @GetMapping("/doctor/{id}")
    @Log(methodName = LOG_QUERY,desc = "查询医生")
    public R<DoctorResponse> queryDoctor(@PathVariable Long id) {
        DoctorResponse doctorResponse = doctorApplicationService.queryDoctor(id);
        return R.ok(doctorResponse);
    }
    @Log(methodName = LOG_QUERY,desc = "分页查询医生")
    @PostMapping("/listDoctor")
    public R<Page<DoctorResponse>> listDoctor(@Valid @RequestBody ListDoctorRequest request) {
        Page<DoctorResponse> doctorResponses = doctorApplicationService.listDoctor(request);
        return R.ok(doctorResponses);
    }
    @Log(methodName = LOG_INSERT,desc = "新增医生")
    @PostMapping("/addDoctor")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = ADD_DOCTOR)
    public R<Boolean> addDoctor(@Validated(Add.class) @RequestBody DoctorRequest request) {
        Boolean isOk = doctorApplicationService.addDoctor(request);
        return R.ok(isOk);
    }
    @Log(methodName = LOG_UPDATE,desc = "更新医生")
    @PostMapping("/updateDoctor")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = REMOVE_DOCTOR)
    public R<Boolean> updateDoctor(@RequestBody @Validated(Update.class) DoctorRequest request) {
        Boolean isOk = doctorApplicationService.updateDoctor(request);
        return R.ok(isOk);
    }
    @Log(methodName = LOG_REMOVE,desc = "删除医生")
    @PostMapping("/removeDoctor")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = REMOVE_DOCTOR)
    public R<Boolean> removeDoctor(@RequestBody @Valid BaseRemove baseRemove) {
        Boolean isOk = doctorApplicationService.removeDoctor(baseRemove.getIds());
        return R.ok(isOk);
    }

    /**
     * 条件查询不分页
     *
     * @return
     */
    @PostMapping("/listDoctorByCondition")
    public R<List<OptionResp> > listDoctorByCondition(@RequestBody ListDoctorRequest request){
        List<OptionResp>  responseList = doctorApplicationService.listDoctorByCondition(request);
        return R.ok(responseList);
    }




}
